﻿using System.Data;
using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using GlxRRSDB.Data;
using GlxRRSDB.Utils;

namespace GlxRRSDB
{
    public partial class ReportDataSet
    {
        partial class CinemaSessionViewDataTable
        {
        }
    
        partial class tblTicketRevenueDataTable
        {
        }

        public static bool init(string connectionString)
        {
            Properties.Settings.Default["ReportDBConnectionString"] = connectionString;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                try
                {
                    conn.Open(); // throws if invalid 
                    //Call Save to persist the settings.  
                    Properties.Settings.Default.Save();
                    return true;
                }
                catch (Exception ex)
                {

                }
            }
            return false;
        }
        partial class tblFilmRevenueDataTable
        {
        }
    }
}

namespace GlxRRSDB.ReportDataSetTableAdapters
{
    partial class tblTicketRevenueTableAdapter
    {
        public int Insert(ImportTicketTypeData data)
        {
            return Insert(data.CinemaID,data.Adult2D,data.Child2D,data.Senior2D,data.Student2D,data.CompTotal2D,data.Revenue2D,data.HappyDay2D,data.Standard3D,data.VIP3D,data.HappyDay3D,data.Revenue3D,data.Trans,data.RevenueConcession,data.Date);
        }
    }

    partial class CinemaFilmTargetViewTableAdapter
    {
    }

    public partial class tblFilmRevenueTableAdapter
    {
        private const string sql = "SELECT CinemaID, SUM(Admission) AS TotalAdmission " +
                                    "FROM tblFilmRevenue " +
                                    "WHERE (DATEDIFF(day, (@fromdate), DATE) >= 0) AND (DATEDIFF(day, (@todate), DATE) <= 0)" +
                                    "GROUP BY CinemaID";
        public Dictionary<int, DailyReportData> getTotalAdmissionEx(DateTime fromdate, DateTime todate, Dictionary<int, DailyReportData> dict)
        {
            getCurrentDateAdmissionEx(fromdate, dict);
            getCurrentWeekAdmissionEx(fromdate, dict);
            getCurrentMonthAdmissionEx(fromdate, dict);
            getCurrentQuarterAdmissionEx(fromdate, dict);
            getCurrentYearAdmissionEx(fromdate, dict);
            return dict;
        }
        public Dictionary<int, DailyReportData> getCurrentDateAdmissionEx(DateTime fromdate, Dictionary<int, DailyReportData> dict)
        {
            SqlDataReader reader;
            SqlCommand cmd = new SqlCommand(sql);
            cmd.Connection = this.Connection;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add(new SqlParameter("@fromdate", SqlDbType.DateTime));
            cmd.Parameters.Add(new SqlParameter("@todate", SqlDbType.DateTime));
            //
            cmd.Parameters[0].Value = fromdate;
            cmd.Parameters[1].Value = fromdate;
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                cmd.Connection.Open();
            }
            reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    int cinemaID = reader.GetInt32(0);
                    //
                    if (dict.ContainsKey(cinemaID))
                    {
                        DailyReportData data = dict[cinemaID];
                        data.currentDate = (double)reader.GetInt32(1);
                        //dict.Add(data.CinemaID, data);
                    }
                    else
                    {
                        DailyReportData data = new DailyReportData();
                        data.CinemaID = cinemaID;
                        data.currentDate = (double)reader.GetInt32(1);
                        dict.Add(data.CinemaID, data);
                    }
                }
            }
            reader.Close();

            return dict;
        }
        public Dictionary<int, DailyReportData> getCurrentWeekAdmissionEx(DateTime fromdate, Dictionary<int, DailyReportData> dict)
        {
            SqlDataReader reader;
            SqlCommand cmd = new SqlCommand(sql);
            cmd.Connection = this.Connection;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add(new SqlParameter("@fromdate", SqlDbType.DateTime));
            cmd.Parameters.Add(new SqlParameter("@todate", SqlDbType.DateTime));
            //
            cmd.Parameters[0].Value = fromdate.StartOfWeek(DayOfWeek.Friday);
            cmd.Parameters[1].Value = fromdate.EndOfWeek(DayOfWeek.Friday);
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                cmd.Connection.Open();
            }
            reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    int cinemaID = reader.GetInt32(0);
                    //
                    if (dict.ContainsKey(cinemaID))
                    {
                        DailyReportData data = dict[cinemaID];
                        data.currentWeek = (double)reader.GetInt32(1);
                        //dict.Add(data.CinemaID, data);
                    }
                    else
                    {
                        DailyReportData data = new DailyReportData();
                        data.CinemaID = cinemaID;
                        data.currentWeek = (double)reader.GetInt32(1);
                        dict.Add(data.CinemaID, data);
                    }
                }
            }
            reader.Close();

            return dict;
        }
        public Dictionary<int, DailyReportData> getCurrentMonthAdmissionEx(DateTime fromdate, Dictionary<int, DailyReportData> dict)
        {
            SqlDataReader reader;
            SqlCommand cmd = new SqlCommand(sql);
            cmd.Connection = this.Connection;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add(new SqlParameter("@fromdate", SqlDbType.DateTime));
            cmd.Parameters.Add(new SqlParameter("@todate", SqlDbType.DateTime));
            //
            cmd.Parameters[0].Value = fromdate.StartOfMonth();
            cmd.Parameters[1].Value = fromdate.EndOfMonth();
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                cmd.Connection.Open();
            }
            reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    int cinemaID = reader.GetInt32(0);
                    //
                    if (dict.ContainsKey(cinemaID))
                    {
                        DailyReportData data = dict[cinemaID];
                        data.currentMonth = (double)reader.GetInt32(1);
                        //dict.Add(data.CinemaID, data);
                    }
                    else
                    {
                        DailyReportData data = new DailyReportData();
                        data.CinemaID = cinemaID;
                        data.currentMonth = (double)reader.GetInt32(1);
                        dict.Add(data.CinemaID, data);
                    }
                }
            }
            reader.Close();

            return dict;
        }
        public Dictionary<int, DailyReportData> getCurrentQuarterAdmissionEx(DateTime fromdate, Dictionary<int, DailyReportData> dict)
        {
            SqlDataReader reader;
            SqlCommand cmd = new SqlCommand(sql);
            cmd.Connection = this.Connection;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add(new SqlParameter("@fromdate", SqlDbType.DateTime));
            cmd.Parameters.Add(new SqlParameter("@todate", SqlDbType.DateTime));
            //
            cmd.Parameters[0].Value = fromdate.StartOfQuarter();
            cmd.Parameters[1].Value = fromdate.EndOfQuarter();
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                cmd.Connection.Open();
            }
            reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    int cinemaID = reader.GetInt32(0);
                    //
                    if (dict.ContainsKey(cinemaID))
                    {
                        DailyReportData data = dict[cinemaID];
                        data.currentQuarter = (double)reader.GetInt32(1);
                        //dict.Add(data.CinemaID, data);
                    }
                    else
                    {
                        DailyReportData data = new DailyReportData();
                        data.CinemaID = cinemaID;
                        data.currentQuarter = (double)reader.GetInt32(1);
                        dict.Add(data.CinemaID, data);
                    }
                }
            }
            reader.Close();

            return dict;
        }
        public Dictionary<int, DailyReportData> getCurrentYearAdmissionEx(DateTime fromdate, Dictionary<int, DailyReportData> dict)
        {
            SqlDataReader reader;
            SqlCommand cmd = new SqlCommand(sql);
            cmd.Connection = this.Connection;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add(new SqlParameter("@fromdate", SqlDbType.DateTime));
            cmd.Parameters.Add(new SqlParameter("@todate", SqlDbType.DateTime));
            //
            cmd.Parameters[0].Value = fromdate.StartOfYear();
            cmd.Parameters[1].Value = fromdate.EndOfYear();
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                cmd.Connection.Open();
            }
            reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    int cinemaID = reader.GetInt32(0);
                    //
                    if (dict.ContainsKey(cinemaID))
                    {
                        DailyReportData data = dict[cinemaID];
                        data.currentYear = (double)reader.GetInt32(1);
                        //dict.Add(data.CinemaID, data);
                    }
                    else
                    {
                        DailyReportData data = new DailyReportData();
                        data.CinemaID = cinemaID;
                        data.currentYear = (double)reader.GetInt32(1);
                        dict.Add(data.CinemaID, data);
                    }
                }
            }
            reader.Close();

            return dict;
        }
    }
}
